import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.awt.*;
import javax.swing.ImageIcon;
//import com.brackeen.javagamebook.graphics.ScreenManager;



/**
 * @author  8100556811
 */
public abstract class GameCore {
	
	protected static final int FONT_SIZE=30;
	protected AudioClip ac;
	
	private static final DisplayMode POSSIBLE_MODES[] ={
		
		//new DisplayMode(1280,1024,16,0),
		new DisplayMode(1280,960,24,0),
//		new DisplayMode(1280,800,16,0),
		new DisplayMode(1024,768,16,0),
		new DisplayMode(800,600,16,0),
    	new DisplayMode(800,600,24,0),
    	new DisplayMode(800,600,16,0),
    	new DisplayMode(1024,768,32,0)
    	
    };
	protected boolean isRunning;
	/**
	 * @uml.property  name="screen"
	 * @uml.associationEnd  
	 */
	protected ScreenManager screen;
	//Signals the game loop that it's time to quit
	/**
	 * @uml.property  name="inputManager"
	 * @uml.associationEnd  
	 */
	protected InputManager inputManager;
	/**
	 * @uml.property  name="newgame"
	 * @uml.associationEnd  
	 */
	protected GameAction newgame;
	 
	public void stop()
	{
		isRunning=false;
	}
 
	//calls int() and gameloop()
	
	public void run()
	{
		try{
			
			init();
			
			URL url = new URL("file:images/sound/maintheme.wav");
            ac = Applet.newAudioClip(url);
            ac.loop();
			main();
			
            gameLoop();
			ac.stop();
		}
		 catch (Exception e) {
	            System.out.println(e);
	          }
		finally{
			screen.restoreScreen();
			
		}
	}
	
	//Sets Fill mode and initiaties and objects
	
	public void init()
	{
		screen = new ScreenManager();
		DisplayMode displayMode = screen.findFirstCompatibleMode(POSSIBLE_MODES);
		screen.setFullScreen(displayMode);
		
		Window window = screen.getFullScreenWindow();
		
		inputManager = new InputManager(window);
		window.setFont(new Font("Dialog",Font.PLAIN,FONT_SIZE));
		window.setBackground(Color.blue);
		window.setForeground(Color.white);
		isRunning=true;
	}
	
	public Image loadImage(String fileName) {
	        return new ImageIcon(fileName).getImage();
	}
	
	//runs the game loop until stop() is called
	
	public void gameLoop()
	{
		long startTime = System.currentTimeMillis();
		long currTime = startTime;
				
		while (isRunning)
		{
			long elapsedTime = System.currentTimeMillis() - currTime;
			currTime+= elapsedTime;
			
			//update
			update(elapsedTime);
			//draw
			Graphics2D g = screen.getGraphics();
			draw(g);
			g.dispose();
			screen.update();
			//take a nap
			try{
				Thread.sleep(20);
			}
			catch (InterruptedException ex){}
						
		}
				
	}
//override
	public void main()
	{
				
	}
	
	
	//updates the state of the game based on the amount of elapsed time that has passed
	
public void update(long elapsedTime)
{
	//do nothing?
}


//draws to the screen , subclasses must override this method
public abstract void draw(Graphics2D g);

}
